//
//  SGGameFinishedView.m
//  EatNumber
//
//  Created by shaozg on 12/23/14.
//  Copyright (c) 2014 kuwo.cn. All rights reserved.
//

#import "SGGameFinishedView.h"
#import "SGCategory.h"
#import "WXApi.h"
#import "SGActionSheet.h"
#import "ShareManager.h"
#import "SGUMOnlineConfig.h"

// 按钮的高宽都是80
#define BUTTON_SIZE     80

@interface SGGameFinishedView () <SGActionSheetDelegate> {
    UIButton *_gobackBtn;   // 返回按钮
    UIButton *_restartBtn;  // 重新开始或者过关按钮
    UIButton *_shareBtn;
    
    UILabel *_tipInfoLabel; // 过关提示
    
    GameProgress _gameProgress;
    
    SGActionSheet *_shareActionSheet;
}

@end

@implementation SGGameFinishedView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubViews];
    }
    
    return self;
}

- (void)initSubViews {
    _tipInfoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    [_tipInfoLabel setBackgroundColor:[UIColor clearColor]];
    [_tipInfoLabel setTextColor:[UIColor whiteColor]];
    [_tipInfoLabel setTextAlignment:NSTextAlignmentCenter];
    _tipInfoLabel.numberOfLines = 0;
    [_tipInfoLabel setFont:[UIFont fontWithName:@"AvenirNextCondensed-Heavy" size:25]];
    _tipInfoLabel.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 4);
    [self addSubview:_tipInfoLabel];
    
    CGRect frame = CGRectMake(0, 0, BUTTON_SIZE, BUTTON_SIZE);
    _gobackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_gobackBtn setFrame:frame];
//    [_gobackBtn setBackgroundColor:[UIColor blackColor]];
    [_gobackBtn setBackgroundImage:[UIImage imageNamed:@"reselect_game"] forState:UIControlStateNormal];
    _gobackBtn.layer.cornerRadius = BUTTON_SIZE / 2.0f;
    _gobackBtn.center = CGPointMake(SCREEN_WIDTH / 4, SCREEN_HEIGHT / 2);
    [_gobackBtn addTarget:self action:@selector(btnClickEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_gobackBtn];
    
    _restartBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_restartBtn setFrame:frame];
//    [_restartBtn setBackgroundColor:[UIColor blackColor]];
    [_restartBtn setBackgroundImage:[UIImage imageNamed:@"restart_game"] forState:UIControlStateNormal];
    _restartBtn.layer.cornerRadius = BUTTON_SIZE / 2.0f;
    _restartBtn.center = CGPointMake(SCREEN_WIDTH * 3 / 4, SCREEN_HEIGHT / 2);
    [_restartBtn addTarget:self action:@selector(btnClickEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_restartBtn];
    
    _shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_shareBtn setFrame:frame];
    UIImage *image = [UIImage imageNamed:@"share_btn"];
    [_shareBtn setImage:image forState:UIControlStateNormal];
    [_shareBtn setSize:image.size];
    _shareBtn.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT * 3 / 4);
    [_shareBtn addTarget:self action:@selector(btnClickEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_shareBtn];
}

- (void)btnClickEvent:(id)sender {
    if (_restartBtn == sender) {
        if ([_delegate respondsToSelector:@selector(restart:)]) {
            [_delegate restart:self];
        }
    } else if (_gobackBtn == sender) {
        if ([_delegate respondsToSelector:@selector(goBack:)]) {
            [_delegate goBack:self];
        }
    } else if (_shareBtn == sender) {
        if (!_shareActionSheet) {
            NSArray * arrname = @[@"微信好友", @"微信朋友圈", @"QQ好友", @"QQ空间"];
            NSArray * stricon = [[NSArray alloc] initWithObjects: @"weixinShare", @"weixinFriendsShare", @"qqFriend", @"qqZone", nil];
            _shareActionSheet = [[SGActionSheet alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 320, SCREEN_WIDTH, SCREEN_WIDTH) rowCount:2];
            [_shareActionSheet setContentWithImageArray:stricon titleArray:arrname];
            _shareActionSheet.delegate = self;
        }
        
        [_shareActionSheet showInView:self];
    }
}

- (void)updateGameProgress:(GameProgress)progress {
    _gameProgress = progress;
    
    if (_gameProgress == GameProgress_PlayerWin) {
        [_restartBtn setBackgroundImage:[UIImage imageNamed:@"player_win"] forState:UIControlStateNormal];
        [_tipInfoLabel setText:@"你真牛！"];
        self.backgroundColor = COLOR_WITH_RGB(0x8ba344);
    } else if (_gameProgress == GameProgress_PlayerFail) {
        [_restartBtn setBackgroundImage:[UIImage imageNamed:@"restart_game"] forState:UIControlStateNormal];
        [_tipInfoLabel setText:@"再想想？"];
        self.backgroundColor = COLOR_WITH_RGB(0x571815);
    } else if (_gameProgress == GameProgress_AllFinished) {
        [_tipInfoLabel setText:@"全部通关，你真是神仙下凡啊！"];
        self.backgroundColor = COLOR_WITH_RGB(0x8ba344);
        [_restartBtn setBackgroundImage:[UIImage imageNamed:@"restart_game"] forState:UIControlStateNormal];
    }
}

- (BOOL)playerIsWin {
    return _gameProgress == GameProgress_PlayerWin;
}

- (void)showInParentView:(UIView *)theView progress:(GameProgress)progress animate:(BOOL)animate {
    [self updateGameProgress:progress];
    
    [self showOrHideShareBtn];
    
    if (animate) {
        self.alpha = 0;
        CGRect oldRect1 = _gobackBtn.frame;
        CGRect oldRect2 = _restartBtn.frame;
        
        [theView addSubview:self];
        
        _gobackBtn.right = 0;
        _restartBtn.left = SCREEN_WIDTH;
        
        if (progress == GameProgress_PlayerWin) {
            CGAffineTransform transform = CGAffineTransformMakeScale(0.1, 0.1);
            _shareBtn.transform = transform;
        }
        
        [UIView animateWithDuration:0.5 animations:^{
            [_gobackBtn setFrame:oldRect1];
            [_restartBtn setFrame:oldRect2];
            
            if (progress == GameProgress_PlayerWin) {
                _shareBtn.transform = CGAffineTransformMakeScale(1, 1);
            }
            
            self.alpha = 1;
        } completion:^(BOOL finished) {
        }];
    } else {
        
    }
}

- (void)showOrHideShareBtn {
    BOOL enableShare = [SGUMOnlineConfig enableShare];
    _shareBtn.hidden = !enableShare;
}

#pragma mark SGActionSheetDelegate
- (void)didSelectItem:(SGActionSheet *)sheetCell index:(NSInteger)index {
    if (0 == index) {
        // 微信好友
        [self shareToWeiXinFriends:YES];
    } else if (1 == index) {
        // 微信朋友圈
        [self shareToWeiXinFriends:NO];
    } else if (2 == index) {
        // QQ好友
        [self shareToQQFriends:YES];
    } else if (3 == index) {
        // QQ空间
        [self shareToQQFriends:NO];
    }
}

- (void)shareToWeiXinFriends:(BOOL)shareToFriend {
    NSString *url = [SGUMOnlineConfig shareUrl];
    NSString *dataUrl = url;
    NSString *title = [SGUMOnlineConfig shareTitle];
    NSString *desc = [SGUMOnlineConfig shareDesc];
    UIImage *image = SHARE_LOGO;
    [[ShareManager defaultManager] shareWeiXinInAPP:KSShareToWXTypeWebPage title:title url:url dataUrl:dataUrl description:desc image:image isToFriends:shareToFriend withCompletion:^(BOOL isSuccess) {
        // 分享成功!!!
        NSLog(@"分享成功!!!");
    }];
}

- (void)shareToQQFriends:(BOOL)shareToFriend {
    NSString *url = [SGUMOnlineConfig shareUrl];
//    NSString *dataUrl = url;
    NSString *title = [SGUMOnlineConfig shareTitle];
    NSString *desc = [SGUMOnlineConfig shareDesc];
    UIImage *image = SHARE_LOGO;
    [[ShareManager defaultManager] shareQQInAPP:KSShareToQQTypeURL title:title url:url description:desc image:image isToZone:!shareToFriend withCompletion:^(BOOL isSuccess) {
        // 分享成功!!!
        NSLog(@"分享成功!!!");
    }];
}
@end
